<?php

namespace addons\shopro\controller\store;

use addons\shopro\exception\Exception;
use addons\shopro\model\Store as ModelStore;

use addons\shopro\controller\Base as AddonsBase;
use think\Db;
use think\exception\PDOException;
use think\exception\ValidateException;
use addons\shopro\model\User;

class Wmstore extends AddonsBase
{

    protected $noNeedLogin = [];
    protected $noNeedRight = ['*'];


    /**
     * 添加店员
     * 王猛 2022-1-24 11:40:18 
     */
    public function doStoreManager()
    {
        $params = $this->request->post();
        $store_id = $params['store_id'];
        $user_id = $params['user_id'];
        $type = $params['type'];  // add  del
        if(empty($store_id) || empty($user_id)|| empty($type)) $this->error('参数不合法！');

        $find = Db::name('shopro_user_store')
            ->where(['store_id'=> $store_id,'user_id'=>$user_id])->find();

        if($find && $type=='del'){
            $res = Db::name('shopro_user_store')->where(['user_id'=>$user_id,'store_id'=>$store_id])->delete();
        }else{
            if($find && $type=='add'){
                $this->error('已是店员身份，请勿重复添加！');
            }
            $newData = [
                'addtime'=>time(),
                'user_id' => $user_id,
                'store_id' => $store_id
            ];
            $res = Db::name('shopro_user_store')->insert($newData);
        }

        if($res) $this->success('操作成功！');
        $this->error('操作失败！');
    }

    /**
     * 商家店员列表
     * 王猛 2022-1-24 14:56:35
     */
    function storeManagerLists(){
        $user = User::info();
        $store_id = $this->request->param('store_id');
        if(empty($store_id)) $this->error('未选择商家！');
        $res = Db::name('shopro_user_store')
            ->alias('a')
            ->join('user b', 'b.id = a.user_id')
            ->where('store_id', $store_id)
            ->where('a.user_id', '<>', $user->id)
            ->field("b.id,nickname,b.avatar,a.id as shopro_user_store_id")
            ->select();
        if($res) $this->success('',$res);
    }

    /**
     * 商家信息初始化
     * 王猛 2022-1-24 18:34:52
     */
    function storeManagerInit(){
        $user_id = $this->auth->id;
        $res = Db::name('shopro_user_store')
            ->alias('a')
            ->join('shopro_store b', 'b.id = a.store_id')
            ->where('a.user_id', '=', $user_id)
            ->field("b.id as store_id,b.name")
            ->select();
        if($res) $this->success('',$res??[]);
    }



    
}
